﻿using System;
using System.Windows.Forms;
using LIK.NetworkTools;
using LIK.WakeOnLan.Properties;

namespace LIK.WakeOnLan.Forms
{
    public partial class FormSettings : Form
    {
        public FormSettings()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            chckbScanOnStartupOn.Checked = Settings.Default.ScanOnStartupOn;
            this.ReloadNicInterfaces();
        }

        private void ReloadNicInterfaces()
        {
            comboBoxEditAdapters.Properties.Items.Clear();
            int count = 0;
            foreach (var nic in NicEnumerator.Instance)
            {
                comboBoxEditAdapters.Properties.Items.Add(nic);
                if(NicEnumerator.Instance.SelectedNic != null && nic.MacAddress.Equals(NicEnumerator.Instance.SelectedNic.MacAddress))
                {
                    comboBoxEditAdapters.SelectedIndex = count;
                }
                count++;
            }
            if (comboBoxEditAdapters.SelectedIndex < 0 && comboBoxEditAdapters.Properties.Items.Count > 0)
            {
                comboBoxEditAdapters.SelectedIndex = 0;
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            NicEnumerator.Instance.ReloadNetworkInterfaces();
            this.ReloadNicInterfaces();
        }

        private void comboBoxEditAdapters_SelectedIndexChanged(object sender, EventArgs e)
        {
            int counter = 0;
            foreach (var nic in NicEnumerator.Instance)
            {
                if (counter == comboBoxEditAdapters.SelectedIndex)
                {
                    NicEnumerator.Instance.SelectedNic = nic;
                    Settings.Default.SelectedNicMac = nic.MacAddress;
                    Settings.Default.Save();
                    break;
                }
                counter++;
            }
        }

        private void chckbScanOnStartupOn_CheckedChanged(object sender, EventArgs e)
        {
            Settings.Default.ScanOnStartupOn = chckbScanOnStartupOn.Checked;
        }


    }
}
